import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';

@Component({
  selector: 'app-upload-txt',
  templateUrl: './upload-txt.component.html',
  styles: ['>>>.ant-upload-list-picture-card .ant-upload-list-item-info::before{left:0}']
})
export class UploadTxtComponent implements OnInit {
  @Input() arr:any[]
  param: any;
  uploadUrl = '/network/minIo/uploadFile';
  fileList: any[] = [

  ];
  showForm = false;
  handle(event:any){
    if(event.type == 'success'){
      if(event.file.response.status != 200){
        this.msg.error(event.file.response.message)
      }else{
        this.msg.success(event.file.response.message)
      }
      
    }
  }
  resetForm(){
    this.modalRef.destroy('ok');
  }
  submit() {
    let fileUrl = '';
    if (this.fileList.length> 0 && (this.fileList[0].response.status == '200' || this.fileList[0].url)) {
      fileUrl = this.fileList[0].url ||  this.fileList[0].response.data;
    }
    if(fileUrl == ''){
      this.msg.error('未上传txt文件');
      return;
    }
    const body={
      txtUrl:fileUrl,
      sn:this.arr
    }
    const url = '/core/host/sendDeviceTxt';
    const method = 'POST';
    this.http.request<any>(method, url,  {body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success(res.message);
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef
  ) { }

  async ngOnInit() {
    this.showForm = true;
  }


}
